﻿using Morus;
using Morus.Infrastructure;
using System;
using System.Data.Entity.Core.EntityClient;
using System.Globalization;

namespace KAKA.Repository
{
    public class ConnectionString
    {
        private const string _edmMetadataFormat = "{0}\\{1}.csdl|{0}\\{2}.ssdl|{0}\\{3}.msl";
        private const string _csdlFileName = "Model";
        private const string _mslFileName = "Model";
        private const string _ssdlFileName = "Model";
        private const string _edmFilesPath = "|DataDirectory|";

        private readonly EntityConnectionStringBuilder _entityBuilder;

        public ConnectionString(string name)
            : this(new ConfigurationManagerWrapper(), name)
        {
        }

        public ConnectionString(IConfigurationManager configuration, string name)
            : this(configuration, name, null, null, null, null)
        {
        }

        public ConnectionString(IConfigurationManager configuration, string name, string edmFilesPath)
            : this(configuration, name, edmFilesPath, null, null, null)
        {
        }

        public ConnectionString(IConfigurationManager configuration, string name, string edmFilesPath, string ssdlFileName)
            : this(configuration, name, edmFilesPath, ssdlFileName, null, null)
        {
        }

        public ConnectionString(IConfigurationManager configuration, string name, string edmFilesPath, string ssdlFileName, string csdlFileName, string mslFileName)
        {
            Check.Argument.IsNotNull(configuration, "configuration");
            Check.Argument.IsNotEmpty(name, "name");

            edmFilesPath = edmFilesPath.IfEmpty(() => _edmFilesPath);
            ssdlFileName = ssdlFileName.IfEmpty(() => _ssdlFileName);
            csdlFileName = csdlFileName.IfEmpty(() => _csdlFileName);
            mslFileName = mslFileName.IfEmpty(() => _mslFileName);

            string providerConnectionString = configuration.GetConnectionString(name);
            string providerName = configuration.GetProviderName(name);
            string metadata = String.Format(CultureInfo.InvariantCulture,
                                            _edmMetadataFormat,
                                            edmFilesPath,
                                            csdlFileName,
                                            ssdlFileName,
                                            mslFileName);

            _entityBuilder = new EntityConnectionStringBuilder
            {
                ProviderConnectionString = providerConnectionString,
                Provider = providerName,
                Metadata = metadata
            };
        }

        public string Value
        {
            get
            {
                return _entityBuilder.ConnectionString;
            }
        }

        public string ProviderName
        {
            get
            {
                return _entityBuilder.Provider;
            }
        }
    }
}
